package com.maistars.spider.controller;

import com.alibaba.fastjson.JSONObject;
import com.maistars.spider.common.exception.TopicSpiderException;
import com.maistars.spider.repository.dao.SystemDao;
import com.maistars.spider.repository.model.SystemMenuPO;
import com.maistars.spider.util.TreeUtil;
import com.maistars.spider.view.MenuVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sys")
public class SystemController {
    @Resource
    private SystemDao systemDao;

    @GetMapping("/menu")
    @ResponseBody
    public Map<String, Object> menu() throws TopicSpiderException {
        Map<String, Object> map = new HashMap<>(16);
        Map<String,Object> home = new HashMap<>(16);
        Map<String,Object> logo = new HashMap<>(16);
        List<SystemMenuPO> menuList = systemDao.getSystemMenus(0);
        List<MenuVO> menuInfo = new ArrayList<>();
        for (SystemMenuPO e : menuList) {
            MenuVO MenuVO = new MenuVO();
            MenuVO.setId(e.getId());
            MenuVO.setPid(e.getPid());
            MenuVO.setHref(e.getHref());
            MenuVO.setTitle(e.getTitle());
            MenuVO.setIcon(e.getIcon());
            MenuVO.setTarget(e.getTarget());
            menuInfo.add(MenuVO);
        }
        map.put("menuInfo", TreeUtil.toTree(menuInfo, 0L));
        home.put("title","首页");
        home.put("href","/page/welcome-1");//控制器路由,自行定义
        logo.put("title","后台管理系统");
        logo.put("image","/images/back.jpg");//静态资源文件路径,可使用默认的logo.png
        map.put("homeInfo", JSONObject.parseObject("{title: '首页',href: '/page/welcome.html'}"));
        map.put("logoInfo", JSONObject.parseObject("{title: '星辰麦',image: '/images/logo.png'}"));
        return map;
    }

    @GetMapping("/clear")
    @ResponseBody
    public Map<String, Object> clear() throws TopicSpiderException {
        Map<String, Object> map = new HashMap<>(16);
        map.put("code",1);
        map.put("msg","服务端清理缓存成功");
        return map;
    }

}